/*
 * InterruptRoutines.c
 *
 *  Created on: 29/12/2012
 *      Author: Bowmer
 */

#include "inc/hw_types.h"
#include "inc/lm4f120h5qr.h"
#include "driverlib/gpio.h"


extern volatile unsigned long rangeStart, rangeEnd;
extern volatile unsigned int RxBufferIndex, rangeValid, stringAvailable;
extern volatile unsigned char RxBuffer[200];


void Interrupt_Enable(){
	// Sprinkle of in-line assembly to enable processor interrupts
	__asm("cpsie");
}


/*********************************************/
/* Timer1 subtimer A interrupt handler       */
/*                                           */
/* This guy is used with the range finder    */
/*  range request. When Timer1A overflows    */
/*  this interrupt is called, and turns the  */
/*  trigger pin off. This concludes the      */
/*  range finder start pulse sequence        */
/*********************************************/

void TIMER1_A_INT(void){
	unsigned long TimerStatus;

	// Get the interrupt status
	TimerStatus = WTIMER1_MIS_R;

	// Clear the asserted interrupts
	WTIMER1_ICR_R = TimerStatus;

	// Turn the pin off
	GPIO_PORTC_DATA_R &= ~GPIO_PIN_6;
}  //TIMER1_A_INT


/*********************************************/
/* Timer1 subtimer B interrupt handler       */
/*                                           */
/* This is used to measure the range pulse   */
/*  duration. The interrupt is triggered on  */
/*  both rising and falling edges. The last  */
/*  three state changes are measured, and    */
/*  the closest two together are assumed to  */
/*  be the true pulse width. A smarter way   */
/*  to do this would be to properly record   */
/*  which edge is which.                     */
/*********************************************/

void TIMER1_B_INT(void){
	unsigned long TimerStatus;

	// Get the interrupt status
	TimerStatus = WTIMER1_MIS_R;

	// Clear the asserted interrupts
	WTIMER1_ICR_R = TimerStatus;

	if(GPIO_PORTC_DATA_R & GPIO_PIN_7){
		rangeValid = 0;
		rangeStart = WTIMER1_TBR_R;
	}
	else{
		rangeValid = 1;
		rangeEnd = WTIMER1_TBR_R;
	}

}  //TIMER1_B_INT


void TIMER2_A_INT(void){
	unsigned long TimerStatus;

	// Get the interrupt status
	TimerStatus = WTIMER2_MIS_R;

	// Clear the asserted interrupts
	WTIMER2_ICR_R = TimerStatus;


//	updateServos(globalSlewAngle, globalLuffAngle);
}	//TIMER2_A_INT


/* UART4 Interrupt Handler */
void UART4_INT(void){
	unsigned long UART4_Status;
	unsigned int j = 0;
	unsigned char character = ' ';

	//Disable UART4 interrupts while in the interrupt handler
	NVIC_EN1_R &= ~NVIC_EN0_INT28;

	//Get the interrupt status
	UART4_Status = UART4_MIS_R;

	//Clear the asserted interrupts
	UART4_ICR_R = UART4_Status;

	while(j <= 200){
		while(UART4_FR_R & UART_FR_RXFE);
		character = UART4_DR_R;
		if(character == 0x0D){
			RxBuffer[j] = character;
			stringAvailable = 1;
			NVIC_EN1_R |= NVIC_EN0_INT28;
			break;
		} else if(character == '$'){
			j=0;
		}
		RxBuffer[j++] = character;
		UART4_DR_R = character;
	}
	NVIC_EN1_R |= NVIC_EN0_INT28;
	stringAvailable = 1;
}//UART4_INT

